﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataManager.TablesManager;

namespace DataManager
{
    public class MessModule
    {
        public class Account
        {
            public bool SigUp(string FullName, string UserName, string Pass)
            {
                try
                {
                    if (Pass.Length < 6) return false;
                    UserName = Library.VieToEng(UserName).ToLower();
                    Pass = Library.EncodePassword(Pass);
                    ACCOUNT ac = new ACCOUNT();
                    ac.FULLNAME = FullName;
                    ac.FRIEND = "";
                    ac.PASS = Pass;
                    ac.USERID = UserName;
                    new AccountManager().Insert<ACCOUNT>(ac);
                    return true;
                }
                catch (Exception ex)
                {
                    Logs.Write(ex);
                    return false;
                }
            }

            public bool Login(string UserName, string Pass)
            {
                try
                {
                    if (Pass.Length < 6) return false;
                    UserName = Library.VieToEng(UserName).ToLower();
                    Pass = Library.EncodePassword(Pass);
                    ACCOUNT ac = new AccountManager().SelectByID<ACCOUNT>(UserName);
                    if (ac != null)
                    {
                        if (ac.PASS.Equals(Pass))
                            return true;
                        else return false;
                    }
                    else return false;
                }
                catch (Exception ex)
                {
                    Logs.Write(ex);
                    return false;
                }
            }

            public bool Add_Friend(string UserName, string Friend)
            {
                try
                {
                    UserName = Library.VieToEng(UserName).ToLower();
                    Friend = Library.VieToEng(Friend).ToLower();
                    if (Check_Friend(Friend))
                    {
                        if (Check_Friend(UserName))
                        {
                            ACCOUNT acUpdate = new AccountManager().SelectByID<ACCOUNT>(UserName);
                            if (acUpdate != null)
                            {
                                acUpdate.FRIEND += ("||" + Friend + "||");
                                new AccountManager().Update(acUpdate);
                                return true;
                            }
                            else return false;

                        }
                        else return false;
                    }
                    else return false;
                }
                catch (Exception ex)
                {
                    Logs.Write(ex);
                    return false;
                }
            }

            public bool Remove_Friend(string UserName, string Friend)
            {
                try
                {
                    UserName = Library.VieToEng(UserName).ToLower();
                    Friend = Library.VieToEng(Friend).ToLower();
                    if (Check_Friend(Friend))
                    {
                        if (Check_Friend(UserName))
                        {
                            ACCOUNT acUpdate = new AccountManager().SelectByID<ACCOUNT>(UserName);
                            if (acUpdate != null)
                            {
                                acUpdate.FRIEND = acUpdate.FRIEND.Replace("||" + Friend + "||", "");
                                new AccountManager().Update(acUpdate);
                                return true;
                            }
                            else return false;

                        }
                        else return false;
                    }
                    else return false;
                }
                catch (Exception ex)
                {
                    Logs.Write(ex);
                    return false;
                }
            }

            public bool Check_Friend(string Friend)
            {
                try
                {
                    Friend = Library.VieToEng(Friend).ToLower();
                    ACCOUNT acCheck = new AccountManager().SelectByID<ACCOUNT>(Friend);
                    if (acCheck != null) return true;
                    else return false;
                }
                catch (Exception ex)
                {
                    Logs.Write(ex);
                    return false;
                }
            }

            public bool UpdateAccount(string FullName, string UserName, string Pass)
            {
                try
                {
                    UserName = Library.VieToEng(UserName).ToLower();
                    Pass = Library.EncodePassword(Pass);

                    if (Check_Friend(UserName))
                    {
                        ACCOUNT acUpdate = new AccountManager().SelectByID<ACCOUNT>(UserName);
                        if (acUpdate != null)
                        {
                            acUpdate.FULLNAME = FullName;
                            acUpdate.PASS = Pass;
                            acUpdate.USERID = UserName;
                            new AccountManager().Update(acUpdate);
                            return true;
                        }
                        else return false;

                    }
                    else return false;
                }
                catch (Exception ex)
                {
                    Logs.Write(ex);
                    return false;
                }
            }
        }

        public class Messages
        {
            public bool Sent(string UserName, string Friend, string Content, DateTime Time)
            {
                try
                {
                    UserName = Library.VieToEng(UserName).ToLower();
                    Friend = Library.VieToEng(Friend).ToLower();

                    MESS mess = new MESS();
                    mess.MESS_CONTENT = Content;
                    mess.MESS_ID = Library.IDMESS();
                    mess.MESS_STATUS = false;
                    mess.MESS_TIME = Time;
                    mess.USERID_FORM = UserName;
                    mess.USERID_TO = Friend;
                    new MessManager().Insert<MESS>(mess);
                    return true;
                }
                catch (Exception ex)
                {
                    Logs.Write(ex);
                    return false;
                }
            }

            public List<string> Get(string UserName, string Friend)
            {
                try
                {
                    List<string> dataOut = new List<string>();
                    UserName = Library.VieToEng(UserName).ToLower();
                    Friend = Library.VieToEng(Friend).ToLower();

                    string SQL = "SELECT * FROM MESS WHERE MESS.USERID_FORM = '{from}' AND MESS.USERID_TO = '{to}' AND MESS_STATUS = 0";
                    SQL = SQL.Replace("{to}", UserName);
                    SQL = SQL.Replace("{from}", Friend);

                    List<MESS> data  = new MessManager().SelectSQL<MESS>(SQL);
                    for (int i = 0; i < data.Count; i++)
                    {
                        dataOut.Add(data[i].MESS_CONTENT);
                        data[i].MESS_STATUS = true;
                        new MessManager().Update(data[i]);
                    }
                    return dataOut;
                }
                catch (Exception ex)
                {
                    Logs.Write(ex);
                    return new List<string>();
                }
            }
        }
    }
}
